package com.tsd.system.service.impl;

import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysRegUserMapper;
import com.tsd.system.entity.SysRegUser;
import com.tsd.system.entity.SysRegUserExt;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysRegUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRegUser 服务实现层
 *
 * @author Hillpool
 * @date 2022/02/14 15:16:32
 */
@Service
public class SysRegUserServiceImpl extends BaseServiceImpl implements SysRegUserService {

    @Resource
    private SysRegUserMapper sysRegUserMapper;

    @Override
    public SysRegUser getSysRegUserById(Long id) throws Exception {
        return sysRegUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysRegUserExt queryBySid(String sid) throws Exception {
        return sysRegUserMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysRegUser(Long id, SysUserExt opUser) throws Exception {
        SysRegUser old = sysRegUserMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        sysRegUserMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void deleteSysRegUsers(List<String> sids, SysUserExt opUser) throws Exception {
        for (String sid : sids) {
            SysRegUser old = sysRegUserMapper.selectBySid(sid);
            if (old != null) {
                super.autoInjectBaseData(old, opUser, TYPE_DELETE);
                sysRegUserMapper.updateByPrimaryKeySelective(old);
            }
        }
    }

    @Override
    public void saveSysRegUser(SysRegUserExt record, SysUserExt opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            if (!HlpUtils.isEmpty(record.getLogin_id())) {
                SysRegUserExt exist = sysRegUserMapper.selectByLoginId(record.getLogin_id());
                if (exist != null) {
                    throw new HlpException("登录账号：" + record.getLogin_id() + " 已存在");
                }
            }
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            if (!HlpUtils.isEmpty(record.getPassword())) {
                String password = HlpUtils.doMd5(record.getPassword());
                record.setPassword(password);
            }
            sysRegUserMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysRegUserMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveSysRegUser4Anon(SysRegUserExt userExt, SysUserExt opUser) throws Exception {
        super.checkEmpty(userExt.getSid(), "缺少参数：sid");
        SysRegUserExt old = sysRegUserMapper.selectBySid(userExt.getSid());
        super.checkEmpty(old, "注册用户不存在");
        if (!HlpUtils.isEmpty(userExt.getPassword())) {
            userExt.setPassword(HlpUtils.doMd5(userExt.getPassword()));
        }
        super.autoInjectBaseData(userExt, opUser, this.TYPE_UPDATE);
        sysRegUserMapper.updateByPrimaryKeySelective(userExt);
    }

    @Override
    public List<SysRegUserExt> findSysRegUsers(Map<String, Object> params, PageBean page) throws Exception {
        return sysRegUserMapper.selectByPage(params, page);
    }

    @Override
    public List<SysRegUserExt> findSysRegUsers(Map<String, Object> params) throws Exception {
        return sysRegUserMapper.selectByParams(params);
    }

    @Override
    public SysRegUserExt queryValidByOpenId(String openId) throws Exception {
        return sysRegUserMapper.selectValidByOpenId(openId);
    }

    @Override
    public SysRegUserExt queryValidByUnionId(String unionId) throws Exception {
        return sysRegUserMapper.selectValidByUnionId(unionId);
    }

    @Override
    public SysRegUserExt queryValidByUnionIdOrOpenId(String unionId, String openId) throws Exception {
        SysRegUserExt userExt = null;
        if (!HlpUtils.isEmpty(unionId)) {
            userExt = this.queryValidByUnionId(unionId);
        }
        if (userExt == null && !HlpUtils.isEmpty(openId)) {
            userExt = this.queryValidByOpenId(openId);
        }
        return userExt;
    }

    @Override
    public SysRegUserExt regUserLogin(Map<String, Object> map) throws Exception {
        String loginId = HlpUtils.getString(map, "login_id");
        String password = HlpUtils.getString(map, "password");
        super.checkEmpty(loginId, "缺少参数：loginId");
        super.checkEmpty(password, "缺少参数：password");
        SysRegUserExt userExt = sysRegUserMapper.selectByLoginId(loginId);
        if (userExt == null) {
            throw new HlpException("账号不存在");
        }
        if (userExt.getState() != null && userExt.getState() == Constants.USER_STATUS_VALID) {
            String md5 = HlpUtils.doMd5(password);
            if (userExt.getPassword() != null && userExt.getPassword().equals(md5)) {
                userExt.setPassword(null);
                SysRegUserExt data = new SysRegUserExt();
                data.setId(userExt.getId());
                data.setLast_logined_date(new Date());
                sysRegUserMapper.updateByPrimaryKeySelective(data);
            } else {
                throw new HlpException("账号或密码错误");
            }
        } else {
            throw new HlpException("用户账号状态异常");
        }
        return userExt;
    }

    @Override
    public void changeRegUserPwd(Map<String, Object> map, SysUserExt opUser) throws Exception {
        String sid = HlpUtils.getString(map, "sid");
        String password = HlpUtils.getString(map, "password");
        String newPassword = HlpUtils.getString(map, "new_password");
        super.checkEmpty(sid, "缺少参数：sid");
        super.checkEmpty(password, "缺少参数：password");
        super.checkEmpty(newPassword, "缺少参数：new_password");
        SysRegUserExt userExt = sysRegUserMapper.selectBySid(sid);
        super.checkEmpty(userExt, "注册用户无效");
        String pwdMd5 = HlpUtils.doMd5(password);
        if (!userExt.getPassword().equals(pwdMd5)) {
            throw new HlpException("原密码错误");
        }
        SysRegUserExt data = new SysRegUserExt();
        data.setId(userExt.getId());
        data.setSid(userExt.getSid());
        data.setPassword(HlpUtils.doMd5(newPassword));
        super.autoInjectBaseData(data, opUser, this.TYPE_UPDATE);
        sysRegUserMapper.updateByPrimaryKeySelective(data);
    }

    @Override
    public SysRegUserExt getSysRegUserByMobile(String mobile) throws Exception {
        return sysRegUserMapper.selectTopOneByMobile(mobile);
    }
}
